<?php
ob_start();
require('../config.php');

function verify_page(){
	if(isset($_SESSION[sess_login])){
		$name = $_SESSION[sess_login]['username'];
		include('../connect.php');
		$sql = 'SELECT * FROM `user_account` WHERE `username` = "'.$name.'"';
		$result = mysql_query($sql,$conn)or die("ERROR LIB: #01");//SQL Command
		$query_num = mysql_num_rows($result);
		if($query_num==1){	
			return true;
		}
		else{
			return false;
		}
		mysql_free_result($result);
		mysql_close($conn);
	}
	else{
		return false;
	}
}

function gettimeunique(){
	$timezone  = +7; //(GMT +7:00)  
	$time_string_unique = gmdate("YmdHis", time() + 3600*($timezone+date("0"))); 
	return $time_string_unique;
}

function getcurrenttime(){
	$timezone  = +7; //(GMT +7:00)  
	$current_time = gmdate("H:i:s d/m/Y", time() + 3600*($timezone+date("0"))); 
	return $current_time;
}

function read_var ($val){
	foreach($val as $key => $value){
		$var_array[$value['name']] = trim($value['value']);
	}
	return ($var_array);
	
}

function db2txt($dbname,$flag_crop)
{
	$flag_db2txt = false;
	if(is_file("connect.php")){
		$flag_db2txt = true;
		include("connect.php");
	} else if (is_file("../connect.php")){
		$flag_db2txt = true;
		include("../connect.php");
	}
	else {
		$flag_db2txt = false;
		echo 'Cannot find connection'; 
	}
	if($flag_db2txt){
		$sqlgettext = "SELECT txtname FROM db2txt where dbname = '".$dbname."' ";
		$resultgettext = mysql_query($sqlgettext, $conn);
		
		if (mysql_num_rows($resultgettext)){
			while ($rowgettext = mysql_fetch_row($resultgettext)) {
					$satxt=$rowgettext[0];
					if($flag_crop){
						$satxt = catchuoi($satxt,'12');
					}
					return $satxt; 
				}
			}
		else{  
			return $dbname;
		}
		mysql_free_result($resultgettext);	
	}
	mysql_close($conn);
}

function read_db($table,$id)
{
	include("../connect.php");
	$req = '';
	$sql = 'SELECT * FROM '.$table.' where id = "'.$id.'"';
	$result = mysql_query($sql, $conn);
	if (mysql_num_rows($result)){
		while ($row = mysql_fetch_row($result)){
			foreach($row as $key => $val){
				$req .= $val.'/'; 
			}
		}
		return $req;
	}
	else{  
		return 'No data';
	}
	mysql_free_result($resultgettext);	
	mysql_close($conn);
}

function save_log($do,$where,$id) //$do: Add,Update,Edit,Status,Delete 
{ 
	$admin = $_SESSION['adminlog'];
	$req = read_db($where,$id);
	
	$timezone  = +7; //(GMT +7:00)  
	$currenttime = gmdate("Y-m-d H:i:s", time() + 3600*($timezone+date("0"))); 
	$f_log = '_logDo_'.gmdate("y_m", time() + 3600*($timezone+date("0"))); 

	$f = @fopen('../LOG_FILE/'.$f_log, 'a+');
	if ($f) {
		if($do == 'Check'){
		  @fputs($f, "\t".$req);
		} else {
		  @fputs($f, "\n".$currenttime."\t".$_SERVER['REMOTE_ADDR']."\t".$admin."\t".$do."\t".$where."\t".$req);
		}
		@fclose($f);
	}
}

function catchuoi($chuoi,$gioihan)
{
    if(strlen($chuoi)<=$gioihan) {
        return $chuoi;
    }
    else{
        if(strpos($chuoi," ",$gioihan) > $gioihan){
            $new_gioihan=strpos($chuoi," ",$gioihan);
            $new_chuoi = substr($chuoi,0,$new_gioihan)."..";
            return $new_chuoi;
        }
        $new_chuoi = substr($chuoi,0,$gioihan)."..";
        return $new_chuoi;
    }
}

function utf8_to_ascii($str){
if(!$str) return false;
$unicode = array(
	'a'	=>	'á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ',
	'A'	=>	'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ằ|Ẳ|Ẵ|Ặ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
	'd'	=>	'đ',
	'D'	=>	'Đ',
	'e' =>	'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
	'E' =>	'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
	'i'	=>	'í|ì|ỉ|ĩ|ị',	
	'I'	=>	'Í|Ì|Ỉ|Ĩ|Ị',	
	'o'	=>	'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
	'O'	=>	'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
	'u'	=>	'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
	'U'	=>	'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
	'y'	=>	'ý|ỳ|ỷ|ỹ|ỵ',
	'Y'	=>	'Ý|Ỳ|Ỷ|Ỹ|Ỵ'
);
	foreach($unicode as $nonUnicode=>$uni) 
	$str = preg_replace("/($uni)/i",$nonUnicode,$str);
	$str = preg_replace('/[^a-zA-Z0-9\-_]/','',$str);
return $str;
}


ob_end_flush();
?>